 //check robustness to different fixed effects and controls
 
 use "$data_out/provider_propensity.dta", clear




//create variable 
label var ed_prescription "Prescription in ED"
label var prov7 "7 Different Prescribers by 1 Year"
label var day_180 "Probability of 180 Days of Suppply within 1 Year"
label var scripts_365 "Number of Opioid Prescriptions within 1 year"

	foreach outcome in scripts_365 prov7  day_180  {
eststo `outcome'_1:	ivreghdfe `outcome'   (ed_prescription = propensity) , absorb(hym hd) cluster(provID) first
eststo `outcome'_2:	ivreghdfe `outcome' (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female married (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_4:	ivreghdfe `outcome' i.age_bin race_white female  college married (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first	
eststo `outcome'_5:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p  (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_6:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p   (ed_prescription = propensity) , absorb(hym hd i.diag mos) cluster(provID) first

}

		

	

cap confirm file "$data_out/Promotions.dta" 
if _rc!=0 { 
do "$Outcomes/Promotions.do"
}
else use "$data_out/Promotions.dta" , clear

label var ed_prescription "Prescription in ED"
label var promotion1 "Probability of Promotion within 1 Year"
label var promotion2 "Probability of Promotion within 2 Years"
label var demotion1 "Probability of Demotion within 1 Year"
label var demotion2 "Probability of Demotion within 2 Years"



foreach outcome in promotion1   {
eststo `outcome'_1:	ivreghdfe `outcome'   (ed_prescription = propensity) , absorb(hym hd) cluster(provID) first
eststo `outcome'_2:	ivreghdfe `outcome' (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female married (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_4:	ivreghdfe `outcome' i.age_bin race_white female  college married (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first	
eststo `outcome'_5:	ivreghdfe `outcome' i.age_bin race_white female i.grade college married longevity afqt_p  (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_6:	ivreghdfe `outcome' i.age_bin race_white female i.grade college married longevity afqt_p   (ed_prescription = propensity) , absorb(hym hd i.diag mos) cluster(provID) first
}

cap confirm file "$data_out/Flags.dta" 
if _rc!=0 { 
do "$Outcomes/Flags.do"
}
else use "$data_out/Flags.dta", clear 

label var ed_prescription "Prescription in ED"
label var any_flag "Any Flag within 1 Year"
label var any_flag2  "Any Flag within 2 Years"
label var substance_flag "Drug or Alcohol Flag within 1 Year"
label var substance_flag2 "Drug or Alcohol Flag within 2 Years"
label var adverse_flag "Discipline Flag within 1 Year"
label var adverse_flag2 "Discipline Flag within 2 Years"



foreach outcome in  adverse_flag {
eststo `outcome'_1:	ivreghdfe `outcome'   (ed_prescription = propensity) , absorb(hym hd) cluster(provID) first
eststo `outcome'_2:	ivreghdfe `outcome' (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female married (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_4:	ivreghdfe `outcome' i.age_bin race_white female  college married (ed_prescription = propensity) , absorb(hym i.diag) cluster(provID) first	
eststo `outcome'_5:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) , absorb(hym hd i.diag) cluster(provID) first
eststo `outcome'_6:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) , absorb(hym hd i.diag mos) cluster(provID) first

}

//Tables
label var ed_prescription "Prescriptions in 1 Year"
	
	#delimit ;

esttab scripts_365_1 scripts_365_2 scripts_365_3 scripts_365_4 scripts_365_5 scripts_365_6 using "robustness.rtf" , 
	replace label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
		keep(ed_prescription)
		nomtitles
		title(Specification Table);
#delimit cr

label var ed_prescription "7 Providers in 1 Year"
	
	#delimit ;

esttab prov7_1 prov7_2 prov7_3 prov7_4 prov7_5 prov7_6 using "robustness.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr


label var ed_prescription "180 Days of Supply in 1 Year"
	
	#delimit ;

esttab day_180_1 day_180_2 day_180_3 day_180_4 day_180_5 day_180_6 using "robustness.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

label var ed_prescription "Promotion in 1 Year"
	
	#delimit ;

esttab promotion1_1 promotion1_2 promotion1_3 promotion1_4 promotion1_5 promotion1_6 using "robustness.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

label var ed_prescription "Adverse Flag in 1 Year (Army Only)"
	
	#delimit ;

esttab adverse_flag_1 adverse_flag_2 adverse_flag_3 adverse_flag_4 adverse_flag_5 adverse_flag_6 using "robustness.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

